Challenge: MS-Word Comparison

Anyone interested in writing the following function for me?

void RedLinedTextFiles(string MsWord, NameFile1, NameFile2)
{ // Invoke the MsWord program (="c:\Program Files\...\winword.exe")
// to use the 'Compare' documents feature to display the
// red-lined differences between the two named Windows files,
// which are presumably Notepad.exe friendly ASCII files.
// If Word is already running ignore it, and start a new window

// That is, when done MS-Word is showing the differences.
// Note: pretty sure a few 'oleMethod' commands will be needed.
// Its important to ignore existing MS_Word running, if possible
// I have a suspision that we may need the name of a temporary file,
// to prevent MS-Word from accidentally saving and erasing one of the
// Original files. If so, add a parameter for NameTempFile
// (perhaps its c:\Temp\dp1.txt)

} // end RedLinedTextFiles
llandale - Wed Feb 25 14:19:22 EST 2009

Re: Challenge: MS-Word Comparison
ePiallat - Fri Feb 27 10:16:36 EST 2009

/* Here you are !

– Éric */
const int wdFormatText = 2
const int wdFormatRTF = 6
void RedLinedTextFiles(string NameFile1, NameFile2, ResultFile)
{
OleAutoArgs theArgs = create
OleAutoObj theDocument, theDocuments
OleAutoObj theWordApp = oleCreateAutoObject("Word.Application")
olePut (theWordApp, "visible", true)
oleGet (theWordApp, "Documents", theDocuments)
put (theArgs, "FileName", NameFile1)
oleMethod(theDocuments, "Open", theArgs, theDocument)
clear theArgs
put (theArgs, "Name", NameFile2)
oleMethod(theDocument, "Compare", theArgs)
clear theArgs
put (theArgs, "FileName", ResultFile)
put (theArgs, "FileFormat", wdFormatRTF)
oleMethod(theDocument, "SaveAs", theArgs)
delete theArgs
} // end RedLinedTextFiles

RedLinedTextFiles ("c:/temp/doc1.txt", "c:/temp/doc2.txt", "c:/temp/dp1.rtf")

Re: Challenge: MS-Word Comparison
llandale - Fri Feb 27 15:34:06 EST 2009

ePiallat - Fri Feb 27 10:16:36 EST 2009
/* Here you are !

– Éric */
const int wdFormatText = 2
const int wdFormatRTF = 6
void RedLinedTextFiles(string NameFile1, NameFile2, ResultFile)
{
OleAutoArgs theArgs = create
OleAutoObj theDocument, theDocuments
OleAutoObj theWordApp = oleCreateAutoObject("Word.Application")
olePut (theWordApp, "visible", true)
oleGet (theWordApp, "Documents", theDocuments)
put (theArgs, "FileName", NameFile1)
oleMethod(theDocuments, "Open", theArgs, theDocument)
clear theArgs
put (theArgs, "Name", NameFile2)
oleMethod(theDocument, "Compare", theArgs)
clear theArgs
put (theArgs, "FileName", ResultFile)
put (theArgs, "FileFormat", wdFormatRTF)
oleMethod(theDocument, "SaveAs", theArgs)
delete theArgs
} // end RedLinedTextFiles

RedLinedTextFiles ("c:/temp/doc1.txt", "c:/temp/doc2.txt", "c:/temp/dp1.rtf")

Great. The comparison results look right and end up in 'Document 1', which is desirable.

However, maybe things are different on my machine. I notice that the Results file is ALSO displayed, and its really just File1. When I comment out the last 3 lines (2 puts, 1 oleMethod) that 'SaveAs', I end up with Document 1 and File 1; no doubt your 'SaveAs' is applying to the original document (theDocument) instead of the new comparison document.

I close the File1 by moving this line up after the clear(theArgs) command:
oleMethod(theDocument, "Close", theArgs) // Close File1

That;s pretty good, maybe I don't really want to save the results in a report file, but my bigger program should probably allow for that.

This works whether or not MS-Word is currently running, that's also good.

But so far I don't know how to save the displayed 'Document 1' into the named report file name. If we run it again we get 'Document 2', so we cannot use 'Document 1' as some sort of key. Too bad the 'Compare' oleMethod doesn't allow a return parameter, a handle on the OleAutoObj. If we had a handle on the 'compare' instance, we can SaveAs that handle.

Had a fiasco opening the files 'Visible' false. Couldn't see them, couldn't get a handle on them to close, had to rudely close with Task Manager.

Had some luck defining a function that takes a string parameter, and if not null issues 'warn' and the message. Embedd the various 'oleMethod' etc commands (which return string error messages) in a call to this function, we get to see the errors those calls make, since 'warn' prints a backtrace including the line number of the calling OLE command.

Also, My MS-Word was configured to put deletions in the right margin, much like comments. Using the Word GUI Tools Menu >>Options... >>Track Changes tab, I changed the 'Use Balloons' value to 'Only for Comments/Formatting' which made the results look better. While I'm at it, I wouldn't mind changing trhe Insertions to be Underline Blue, Deletions Strikethrough Red, preferably for just this instance of Word. Wondering how to do that.

Attached is where I'm at so far.

>Louie
Attachments

attachment_14222724_Test_fRedLinedTextFiles.dxl

Re: Challenge: MS-Word Comparison
Lee_Bost - Fri Feb 27 15:52:31 EST 2009

llandale - Fri Feb 27 15:34:06 EST 2009
Great. The comparison results look right and end up in 'Document 1', which is desirable.

However, maybe things are different on my machine. I notice that the Results file is ALSO displayed, and its really just File1. When I comment out the last 3 lines (2 puts, 1 oleMethod) that 'SaveAs', I end up with Document 1 and File 1; no doubt your 'SaveAs' is applying to the original document (theDocument) instead of the new comparison document.

I close the File1 by moving this line up after the clear(theArgs) command:
oleMethod(theDocument, "Close", theArgs) // Close File1

That;s pretty good, maybe I don't really want to save the results in a report file, but my bigger program should probably allow for that.

This works whether or not MS-Word is currently running, that's also good.

But so far I don't know how to save the displayed 'Document 1' into the named report file name. If we run it again we get 'Document 2', so we cannot use 'Document 1' as some sort of key. Too bad the 'Compare' oleMethod doesn't allow a return parameter, a handle on the OleAutoObj. If we had a handle on the 'compare' instance, we can SaveAs that handle.

Had a fiasco opening the files 'Visible' false. Couldn't see them, couldn't get a handle on them to close, had to rudely close with Task Manager.

Had some luck defining a function that takes a string parameter, and if not null issues 'warn' and the message. Embedd the various 'oleMethod' etc commands (which return string error messages) in a call to this function, we get to see the errors those calls make, since 'warn' prints a backtrace including the line number of the calling OLE command.

Also, My MS-Word was configured to put deletions in the right margin, much like comments. Using the Word GUI Tools Menu >>Options... >>Track Changes tab, I changed the 'Use Balloons' value to 'Only for Comments/Formatting' which made the results look better. While I'm at it, I wouldn't mind changing trhe Insertions to be Underline Blue, Deletions Strikethrough Red, preferably for just this instance of Word. Wondering how to do that.

Attached is where I'm at so far.

>Louie

It is my understanding that the Word Track Changes options are global to Word and aren't linked to the individual Word instances. So you can change them but it will change for every Word document you view.

Re: Challenge: MS-Word Comparison
llandale - Sun Mar 01 14:15:57 EST 2009

Lee_Bost - Fri Feb 27 15:52:31 EST 2009
It is my understanding that the Word Track Changes options are global to Word and aren't linked to the individual Word instances. So you can change them but it will change for every Word document you view.

Then when I figure out how to set it, I'll figure out how to retrieve it, and put it on the comparison dialog so the user can choose from the dialog rather than messing around with Word options.

Re: Challenge: MS-Word Comparison
ePiallat - Mon Mar 02 04:00:11 EST 2009

llandale - Fri Feb 27 15:34:06 EST 2009
Great. The comparison results look right and end up in 'Document 1', which is desirable.

However, maybe things are different on my machine. I notice that the Results file is ALSO displayed, and its really just File1. When I comment out the last 3 lines (2 puts, 1 oleMethod) that 'SaveAs', I end up with Document 1 and File 1; no doubt your 'SaveAs' is applying to the original document (theDocument) instead of the new comparison document.

I close the File1 by moving this line up after the clear(theArgs) command:
oleMethod(theDocument, "Close", theArgs) // Close File1

That;s pretty good, maybe I don't really want to save the results in a report file, but my bigger program should probably allow for that.

This works whether or not MS-Word is currently running, that's also good.

But so far I don't know how to save the displayed 'Document 1' into the named report file name. If we run it again we get 'Document 2', so we cannot use 'Document 1' as some sort of key. Too bad the 'Compare' oleMethod doesn't allow a return parameter, a handle on the OleAutoObj. If we had a handle on the 'compare' instance, we can SaveAs that handle.

Had a fiasco opening the files 'Visible' false. Couldn't see them, couldn't get a handle on them to close, had to rudely close with Task Manager.

Had some luck defining a function that takes a string parameter, and if not null issues 'warn' and the message. Embedd the various 'oleMethod' etc commands (which return string error messages) in a call to this function, we get to see the errors those calls make, since 'warn' prints a backtrace including the line number of the calling OLE command.

Also, My MS-Word was configured to put deletions in the right margin, much like comments. Using the Word GUI Tools Menu >>Options... >>Track Changes tab, I changed the 'Use Balloons' value to 'Only for Comments/Formatting' which made the results look better. While I'm at it, I wouldn't mind changing trhe Insertions to be Underline Blue, Deletions Strikethrough Red, preferably for just this instance of Word. Wondering how to do that.

Attached is where I'm at so far.

>Louie

I didn't experience any of those issues... On the other hand, I don't have "balloons" either, as we're using an old version of MSWord here (2K).
After comparison, my document window is still identified as "Doc1.txt", that's the reason why I made a "Save to RTF", to keep redlinings. But I have a single document window all the way.

Maybe you could get the compare result from the "Item" method?
Something like that?
/**/
clear theArgs
put (theArgs, 2)
oleMethod(theDocuments,"Item", theArgs, theDocument)
/**/
Of course, I can't test it myself, but I assume that because we created a specific word instance and opened one file only, compare result will be document #2.

Re: Challenge: MS-Word Comparison
ePiallat - Mon Mar 02 04:16:23 EST 2009

ePiallat - Mon Mar 02 04:00:11 EST 2009
I didn't experience any of those issues... On the other hand, I don't have "balloons" either, as we're using an old version of MSWord here (2K).
After comparison, my document window is still identified as "Doc1.txt", that's the reason why I made a "Save to RTF", to keep redlinings. But I have a single document window all the way.

Maybe you could get the compare result from the "Item" method?
Something like that?
/**/
clear theArgs
put (theArgs, 2)
oleMethod(theDocuments,"Item", theArgs, theDocument)
/**/
Of course, I can't test it myself, but I assume that because we created a specific word instance and opened one file only, compare result will be document #2.

After a few googling, I found there was a new optionnal argument on the "compare" method that specifies the target: CompareTarget.

It can take three values:
wdCompareTargetCurrent <=== This is old Word behaviour
wdCompareTargetNew <=== I suppose this is your default
wdCompareTargetSelected
So I suppose my macro would work with:
/**/
clear theArgs
put (theArgs, "Name", NameFile2)
put (theArgs, "CompareTarget", wdCompareTargetCurrent) // single extra line
oleMethod(theDocument, "Compare", theArgs)
/**/

Providing someone knows what is the actual value of "wdCompareTargetCurrent".
As it is the historical default, I would assume:
const int wdCompareTargetCurrent = 0

But can someone check the VBA library about it?

Re: Challenge: MS-Word Comparison
llandale - Mon Mar 02 14:59:40 EST 2009

ePiallat - Mon Mar 02 04:16:23 EST 2009
After a few googling, I found there was a new optionnal argument on the "compare" method that specifies the target: CompareTarget.

It can take three values:
wdCompareTargetCurrent <=== This is old Word behaviour
wdCompareTargetNew <=== I suppose this is your default
wdCompareTargetSelected
So I suppose my macro would work with:
/**/
clear theArgs
put (theArgs, "Name", NameFile2)
put (theArgs, "CompareTarget", wdCompareTargetCurrent) // single extra line
oleMethod(theDocument, "Compare", theArgs)
/**/

Providing someone knows what is the actual value of "wdCompareTargetCurrent".
As it is the historical default, I would assume:
const int wdCompareTargetCurrent = 0

But can someone check the VBA library about it?

wdCompareTargetCurrent seems to have value '1'. Put this in front of the oleMethod compare:

put (theArgs, "CompareTarget", 1) // wdCompareTargetCurrent = 1?; compare in place

Swapped File1 with File2, since the compare seems to presume the open file is the 'newer' and the compare-to file is the 'older'.

Seems to be working. Still more work to go.

>Louie